package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.messages.SystemMsgFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.system.System;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.window.NxWindow;
	import newx.com.control.window.NxWindowEvent;

	/**
	 * 
	 * @author yangcg
	 * 
	 */	
	public class ChristmastimeLogic
	{
		public static var _self:ChristmastimeLogic ; 
		public static function getinstence():ChristmastimeLogic
		{
			if(_self==null)
			{
				_self = new ChristmastimeLogic();
			}
			return _self  ;
		}
		private var _ui:ChristmastimeUI ;
		private var _model:ModelLocator ;
		private var _bindManager:BindManage ; 
		
		/*****/
		private var _indexOne:int = -1;
		private var _indexTwo:int = -1 ; 
		/**活动日期数组***/
		private var _arrDate:Array = [] ; 
		/***今天日期***/
		private var date:Date = new Date ();
		/****/
		private var _arrGetDate:Array = [] ; 
		/***当天时间**/
		private var _todayDate:String = "" ;
		/***获取活动剩余时间**/
		private var _getDay:int =  0 ; 
		public function ChristmastimeLogic()
		{
		}
		public  function  init():void
		{
			_ui = ChristmastimeUI.getInstance();
			_model  = ModelLocator.getInstance() ; 
			_bindManager = BindManage.getInstance() ; 
//			var obj:Object = StaticIni.getIniObj("Christmastime_date");
//			_arrDate = obj["r"].split(",");
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_FEEDBACK_DATE_LIST),getDateArr);
		}
		public function BeforeShow():void
		{
			if(_ui==null)
			{
				_ui = ChristmastimeUI.getInstance();
			}
			_arrGetDate = [] ; 
			addListener();
			initItemGroup();
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_FEEDBACK_DATE_LIST);
			
//			if(_indexTwo==-1)
//			{
//				_indexTwo = _bindManager.BindProp(_model.scene.Role, "Today" ,this,  "getToday");
//			}
			
		}
		private function getDateArr(e:CustomMsgEvent):void
		{
			if(e.pList.length == 0) return ; 
			_arrDate = e.pList ; 
			
			
			if(_indexTwo==-1)
			{
				_indexTwo = _bindManager.BindProp(_model.scene.Role, "Today" ,this,  "getToday");
			}
			
		}
		private function getDate(str_day:String):void
		{
			_arrGetDate =  [] ;
			var str:String  =str_day
			for(var i:int=0 ; i <_arrDate.length ;i++)
			{
				if(_arrDate[i]==str)
				{
					_arrGetDate.push(str);
					return  ; 
				}else
				{
					_arrGetDate.push(_arrDate[i]);
				}
			}
			
		}
		private function addListener():void
		{
			if(_ui==null)
			{
				_ui = ChristmastimeUI.getInstance();
			}
			_ui.itemGroup.addItemsEvent(MouseEvent.CLICK,ClickHandler);
			_ui.itemGroup.addItemsEvent(MouseEvent.MOUSE_OVER,OverkHandler);
			_ui.itemGroup.addItemsEvent(MouseEvent.MOUSE_OUT,OutHandler);
			_ui.closeBtn.addEventListener(MouseEvent.CLICK,closeHandler);
		}
		private function removeListener():void
		{
			_ui.itemGroup.removeItemsEvent(MouseEvent.CLICK,ClickHandler);
		}
		public function AfterHide():void
		{
			if(_indexTwo!=-1)
			{
				_indexTwo = _bindManager.UnbindProp(_model.scene.Role, "Today" , _indexTwo);
			}
			
			for(var i:int = 0 ; i<_ui.itemGroup.col*_ui.itemGroup.row ;i++)
			{
				 var mc :MovieClip = _ui.itemGroup.getItemByIndex(i) as MovieClip ;
				 if( mc["Image"].source!="")
				 {
					 mc["Image"].source =null ;
				 }
				
			}
			unbind();
			removeListener();
		}
		private function setDateItemGroup():void
		{
			var i:int = 0 ; 
			for( i ; i <_ui.itemGroup.col * _ui.itemGroup.row ; i++)
			{
				var ItemMc:MovieClip =_ui.itemGroup.getItemByIndex(i) as MovieClip ; 
//				ItemMc["Image"].source = "" ; 
//				ItemMc["itemGoto"] = 0 ;
//				ItemMc["mc_1"]  = null ; 
				if(i<_arrGetDate.length)
				{
					ItemMc["date"] = _arrGetDate[i] ;   
				}else
				{
					ItemMc["date"]  = 0 ;
				}
//				ItemMc["type"]	= -1 ; 
			}	
		}
		/**
		 * 初始化每个格子
		 */		
		private function initItemGroup():void
		{
			var i:int = 0 ; 
			for( i ; i <_ui.itemGroup.col * _ui.itemGroup.row ; i++)
			{
				var ItemMc:MovieClip =_ui.itemGroup.getItemByIndex(i) as MovieClip ; 
				var Image:NxClip = new NxClip();
				ItemMc.visible  = true  ; 
				ItemMc["img01"].addChild(Image) ; 
				ItemMc["Image"] = Image ; 
				
//				var ItemMc:MovieClip =_ui.itemGroup.getItemByIndex(i) as MovieClip ; 
				ItemMc["Image"].source = "" ; 
				ItemMc["itemGoto"] = 0 ;
				ItemMc["mc_1"]  = null ; 
//				if(i<_arrGetDate.length)
//				{
//					ItemMc["date"] = _arrGetDate[i] ;   
//				}else
//				{
//					ItemMc["date"]  = 0 ;
//				}
				ItemMc["type"]	= -1 ; 
				
			}	
		}
		/**
		 *单击每个圣诞袜子 
		 */		
		private function ClickHandler(e:MouseEvent):void
		{
			var itemMc:MovieClip = e.currentTarget as MovieClip
			if(itemMc["date"]!=0)
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_TAKE_FEEDBACK,int(itemMc["date"]));
			}
		}
		/**
		 *鼠标移上去 
		 */		
		private function OverkHandler(e:MouseEvent):void
		{
			var itemMc:MovieClip 	= e.currentTarget as MovieClip
			var value:int 		  	= itemMc["date"] ; 
			var itemGoto:int     	=itemMc["itemGoto"] ; 
			var mc_1:MovieClip		= itemMc["mc_1"] ; 
			
			if(value == 0 || itemMc["type"] == -1||mc_1 == null){
				return ; 
			}
			var year :int   = value/10000 ; 
			var month:int   = (value%10000)/100 ; 
			var day:int 	=((value%10000)%100) ; 
			
			var MagicCoin:int =Math.ceil(itemMc["MagicCoin"] *0.1);
			var GoldCoin :int = Math.ceil(itemMc["GoldCoin"]*0.1) ; 
			if(MagicCoin<1)
			{
				MagicCoin =1 ; 
			}
			if(GoldCoin<1)
			{
				GoldCoin =1 ; 
			}
			switch (itemGoto)
			{
				case 1:
					mc_1.gotoAndStop(3) ; 
					break ;
				case 2:
					mc_1.gotoAndStop(4) ; 
					break ; 
				
			}
			TipsMainLogic.instance.showString(StaticIni.getSwfLangStrVar("UIString_1000666",
				[year,month,day,itemMc["MagicCoin"],itemMc["GoldCoin"],MagicCoin,GoldCoin]));
			
		}
		/**
		 * 鼠标移除
		 */		
		private function OutHandler(e:MouseEvent):void
		{
			var itemMc:MovieClip 	= e.currentTarget as MovieClip
			var value:int 		  	= itemMc["date"] ; 
			var itemGoto:int     	=itemMc["itemGoto"] ; 
			var mc_1:MovieClip		= itemMc["mc_1"] ; 
			if(value != 0 || itemMc["type"] != -1|| mc_1 != null){
				
				if(itemGoto>0)
				{
					mc_1.gotoAndStop(itemGoto);
				}
				
			}
			
			TipsMainLogic.instance.hide();
		}
//		/**
//		 * 关闭窗口
//		 */
		private function closeHandler(e:MouseEvent):void
		{
			_ui.showOrHide();
		}
		/**
		 *初始化绑定 
		 */		
		private function bind():void
		{
			if(_indexOne==-1)
			{
				_indexOne =_bindManager.BindRecord(_model.scene ,"FeedbackRecord" ,addItem ,removeItem ) ; 
			}
			
		}
		//获取今天的日期
		public function set getToday(value:int ):void
		{
			if(value==0)return ; 
			unbind();
			_todayDate = ""; 
			_todayDate = value.toString()
			getDate(value.toString());
			setDateItemGroup();
			bind();
			getday(value.toString())
			
		}
		// 领奖时间显示
		private function getday(strDay:String):void
		{
			if(strDay=="")return ; 
			var i :int = 0; 
			for(i ; i <_arrDate.length ;i++)
			{
				if(strDay==_arrDate[i])
				{
					_getDay =  int (_arrDate.length - i );
						 
				}
			}
			_ui.nameMC["text"].htmlText = StaticIni.getSwfLangStr("UIString_1000667") +""+StaticIni.getSwfLangStrVar("UIString_1000668" , [_getDay]) ; 
		}
		/**
		 *解除绑定 
		 */		
		private function unbind():void
		{
			if(_indexOne!=-1)
			{
				_indexOne = _bindManager.UnbindRecord("FeedbackRecord" , _indexOne);
			}
		}
		/**
		 *在表里添加属性或者改变属性 
		 */		
		private function addItem(PropertiesName:String,PropertiesValue:Array,data:*):void
		{
			var i:int = 0 ; 
			for( i ; i <_ui.itemGroup.col * _ui.itemGroup.row ; i++)
			{
				var ItemMc:MovieClip =_ui.itemGroup.getItemByIndex(i) as MovieClip ; 
				if(ItemMc["date"] == PropertiesValue[0])
				{
					getAward(ItemMc, PropertiesValue);
				}
			}
		}
		/**
		 * 设计奖品
		 */		
		private function getAward(mc:MovieClip , arr:Array):void
		{
		
			var itemMc:MovieClip = ResManager.newResClass("custSkin_0035")  as MovieClip ; 
			var itemGoto:int = 0 ; 
			if(arr[1]==1) // 领取了
			{
				itemMc.gotoAndStop(2);
				itemGoto =2 ; 
			}else if(arr[1]==0&&_todayDate==mc["date"])//当天永远是打开
			{
				itemMc.gotoAndStop(2);
				itemGoto =2 ; 
			}else if (arr[1]==0) //  没领
			{
				itemMc.gotoAndStop(1);
				itemGoto = 1   ; 
			}
		
			mc["Image"].source	= itemMc ;
			mc["GoldCoin"] 	= arr[2] ; 
			mc["MagicCoin"] 	= arr[3] ;
			mc["type"]			= arr[1] ; 
			
			mc["itemGoto"]		= itemGoto ; 
			mc["mc_1"]			=itemMc   ;
 		}
		/**
		 *在表里改变属性或者删除属性 
		 */		
		private function removeItem(PropertiesName:String,PropertiesValue:Array,data:*):void
		{
			
		}
		
	}
}